﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;



namespace ControlServer {
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    

    public class MvcApplication : System.Web.HttpApplication {
        
        private static ControlPoint.ControlAPI.Main _controlMain;

        public static ControlPoint.ControlAPI.Main ControlMain {
            get {
                return _controlMain;
            }
        }
        
        
        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}", // URL with parameters
                new { controller = "Home", action = "Index"} // Parameter defaults
            );

        }
        protected void Application_End() {
            if (_controlMain != null) {
                //((ControlPoint.ControlAPI.ZWaveDimmer)_controlMain.Devices["zw:21815248:4:38:0"]).Off();
                //_controlMain = null;
            }
        }

        protected void Application_Start() {


            RegisterRoutes(RouteTable.Routes);
            Console.WriteLine("Loading API");
            try {
                _controlMain = new ControlPoint.ControlAPI.Main();
                
                //((ControlPoint.ControlAPI.ZWaveDimmer)_controlMain.Devices["zw:21815248:4:38:0"]).Dim(50);
            } catch (Exception e) {
                Console.WriteLine(e.Message);
            }
            

        }
    }
}